
local lvjie = fk.CreateSkill{
  name = "xiaobai__lvjie",
}

Fk:loadTranslationTable {
  ["xiaobai__lvjie"] = "履阶",
  ["@lvjie-maxCard1"] = "履阶伤害",
  ["@lvjie-maxCard2"] = "履阶装备",
  ["#xiaobai__lvjie-use"] = "履阶：你可以视为使用一张【树上开花】",
  ["#xiaobai__lvjie-move"] = "履阶：你可以移动场上一张牌",
  [":xiaobai__lvjie"] = "每轮每项限一次，①你造成伤害后②装备牌置入你的装备区后，你可以视为使用一张【树上开花】，若你于此时机以此法获得的牌为此前唯一最多，你摸一张牌，"..
                   "然后若你发动过另一项且均以此【树上开花】摸过至少三张牌，你移动场上一张牌。",

  ["$xiaobai__lvjie1"] = "幸履丹墀，敢辞舟楫？犯波澜者真儒事。",
  ["$xiaobai__lvjie2"] = "紫薇高拱五云端，柯叶作槎可登揽。",
}

lvjie:addEffect(fk.Damage,{
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:getMark("lvjie_damage-round") < 1
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = lvjie.name, prompt = "#xiaobai__lvjie-use"})
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "xiaobai__lvjie-using", 1)
    room:useVirtualCard("bogus_flower", nil, player, {player}, lvjie.name)
    room:setPlayerMark(player, "xiaobai__lvjie-using", 0)
    room:setPlayerMark(player, "lvjie_damage-round", 1)
    if player.dead then return end

    local card_num = player:getMark("xiaobai__lvjie_cardnum")      
    if card_num > player:getMark("@lvjie-maxCard1") then
      room:drawCards(player,1,lvjie.name)
      room:setPlayerMark(player,"@lvjie-maxCard1",card_num)
    end
    if player.dead then return end

    local can_move = (player:getMark("@lvjie-maxCard1") == 3 and player:getMark("@lvjie-maxCard2") == 3)
    if can_move then
      local targets = room:askToChooseToMoveCardInBoard(player, {
        prompt = "#xiaobai__lvjie-move", 
        skill_name = lvjie.name, 
        cancelable = true})
      if #targets ~= 0 then
        room:askToMoveCardInBoard(player, {
          target_one = targets[1],
          target_two = targets[2],
          skill_name = lvjie.name})
      end
    end
    room:setPlayerMark(player,"xiaobai__lvjie_cardnum",0)
  end,
})

lvjie:addEffect(fk.AfterCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) or player:getMark("lvjie_cardMove-round") > 0 then return end
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Player.Equip then
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = lvjie.name, prompt = "#xiaobai__lvjie-use"})
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "xiaobai__lvjie-using", 1)
    room:useVirtualCard("bogus_flower", nil, player, {player}, lvjie.name)
    room:setPlayerMark(player,"xiaobai__lvjie-using", 0)
    room:setPlayerMark(player,"lvjie_cardMove-round", 1)
    if player.dead then return end

    local card_num = player:getMark("xiaobai__lvjie_cardnum")
    if card_num > player:getMark("@lvjie-maxCard2") then
      room:drawCards(player, 1, lvjie.name)
      room:setPlayerMark(player, "@lvjie-maxCard2", card_num)
    end
    local can_move = (player:getMark("@lvjie-maxCard1") == 3 and player:getMark("@lvjie-maxCard2") == 3)
    if can_move then
      local targets = room:askToChooseToMoveCardInBoard(player,{
        prompt = "#xiaobai__lvjie-move",
        skill_name = lvjie.name,
        cancelable = true
      })
      if #targets ~= 0 then
        room:askToMoveCardInBoard(player, {
          target_one = targets[1],
          target_two = targets[2],
          skill_name = lvjie.name
        })
      end
    end
    room:setPlayerMark(player, "xiaobai__lvjie_cardnum", 0)
  end,
})

lvjie:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self) and player:getMark("xiaobai__lvjie-using") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local cost_data = {}
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == player.Hand and move.skillName == "bogus_flower_skill" and move.proposer == player then
        for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(cost_data, info.cardId)
        end
      end
    end
    if #cost_data > 0 then
      player.room:setPlayerMark(player, "xiaobai__lvjie_cardnum", #cost_data)
    end
  end
})

return lvjie